package net.sppan.jcrop.index;

import java.io.IOException;

import net.coobird.thumbnailator.Thumbnails;

import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;

/**
 * @author SPPan
 *
 */
public class IndexController extends Controller {
	public void index() {
		render("index.html");
	}
	
	public void upload() throws IOException{
		UploadFile file = getFile();
		String avatarUrl = "/upload/1.jpg";
		String saveFile = PathKit.getWebRootPath() + avatarUrl;
		Thumbnails.of(file.getFile()).width(500).toFile(saveFile);//变为400*300,遵循原图比例缩或放到400*某个高度  
		setSessionAttr("imagePath", saveFile);
		renderJson(avatarUrl);
	}
	public void save() throws IOException{
		int x = getParaToInt("x");
		int y = getParaToInt("y");
		int w = getParaToInt("w");
		int h = getParaToInt("h");
		String path  = getSessionAttr("imagePath").toString();
		String avatarUrl = "/upload/1_new.jpg";
		String saveFile = PathKit.getWebRootPath() + avatarUrl;
		
		Thumbnails.of(path).sourceRegion(x,y,w,h).size(300,300).toFile(saveFile);
		String htmlText = "<img src='"+avatarUrl+"' />";
		
		renderHtml(htmlText);
	}
}



